package com.monise.view;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

import com.monise.calendario.JDateChooser;
import com.monise.dao.DAO.FechamentoCaixaDao;
import com.monise.model.FechamentoCaixa;

public class FrameFechamento extends JInternalFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField txtValor;
	private final JDateChooser txtDataConsulta = new JDateChooser();
	private JTextArea txtObservacao;
	private JButton btnSalvar;
	private JButton btnLimpar;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FrameFechamento frame = new FrameFechamento();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public FrameFechamento() {
		setTitle("Fechamento de Caixa");
		setClosable(true);
		setBounds(100, 100, 459, 255);
		
		JPanel panel = new JPanel();
		
		txtDataConsulta.getCalendarButton().setIcon(new ImageIcon(FrameFechamento.class.getResource("/com/monise/calendario/JDateChooserIcon.gif")));
		txtDataConsulta.setFont(new Font("Tahoma", Font.PLAIN, 11));
		txtDataConsulta.setBounds(240,9, 135, 28);
		panel.add(txtDataConsulta);
		
		JLabel label = new JLabel("Observa\u00E7\u00E3o:");
		
		txtObservacao = new JTextArea();
		
		JLabel label_1 = new JLabel("Valor:");
		label_1.setFont(new Font("Tahoma", Font.PLAIN, 13));
		
		txtValor = new JTextField();
		txtValor.setColumns(10);
		
		JLabel label_2 = new JLabel("Data:");
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGap(0, 492, Short.MAX_VALUE)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(18)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(label)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(txtObservacao, GroupLayout.PREFERRED_SIZE, 291, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(label_1)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(txtValor, GroupLayout.PREFERRED_SIZE, 106, GroupLayout.PREFERRED_SIZE)
							.addGap(33)
							.addComponent(label_2)))
					.addContainerGap(117, Short.MAX_VALUE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGap(0, 130, Short.MAX_VALUE)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(label_1)
						.addComponent(txtValor, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label_2))
					.addGap(36)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(label)
						.addComponent(txtObservacao, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(20, Short.MAX_VALUE))
		);
		panel.setLayout(gl_panel);
		
		btnSalvar = new JButton("Salvar");
		btnSalvar.setIcon(new ImageIcon(FrameFechamento.class.getResource("/com/monise/imagens/Save.png")));
		
		btnLimpar = new JButton("Limpar");
		btnLimpar.setIcon(new ImageIcon(FrameFechamento.class.getResource("/com/monise/imagens/Trash.png")));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(20)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(panel, GroupLayout.PREFERRED_SIZE, 404, GroupLayout.PREFERRED_SIZE)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(btnSalvar, GroupLayout.PREFERRED_SIZE, 103, GroupLayout.PREFERRED_SIZE)
							.addGap(26)
							.addComponent(btnLimpar, GroupLayout.PREFERRED_SIZE, 103, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(113, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 130, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(btnSalvar, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnLimpar, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(34, Short.MAX_VALUE))
		);
		getContentPane().setLayout(groupLayout);
		
		btnSalvar.addActionListener(new SalvarFechamento());
		btnLimpar.addActionListener(new LimparCampos());

	}
	
	private class SalvarFechamento implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			
			java.sql.Date data = new java.sql.Date(txtDataConsulta.getDate().getTime());  
			
			FechamentoCaixa fechamentoCaixa = new FechamentoCaixa();
			fechamentoCaixa.setDataFechamento(data);
			fechamentoCaixa.setDescricao(txtObservacao.getText());
			String valorCaixa = txtValor.getText().replace(",", "");
			fechamentoCaixa.setValorFechamento(new BigDecimal(valorCaixa));
			
			FechamentoCaixaDao fechamentoDao = new FechamentoCaixaDao();
			
			try{
				fechamentoDao.save(fechamentoCaixa);
				JOptionPane.showMessageDialog(btnSalvar, "Fechamento de Caixa salvo com sucesso!");
				limpar();
			}catch(Exception ex){
				JOptionPane.showMessageDialog(btnSalvar, "Ocorreu um erro no momento de salvar!");
			}
		}
	}
	
	private class LimparCampos implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			limpar();
		}
	}
	
	private void limpar(){
		txtDataConsulta.setToolTipText("");
		txtObservacao.setText("");
		txtValor.setText("");
	}

}
